//
//  SunTools.m
//  playInBackground
//
//  Created by blank on 15/11/9.
//  Copyright © 2015年 blank. All rights reserved.
//  封装百度语音,百度定位,

#import "SunTools.h"
#import "BDTTSSynthesizer.h"
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Search/BMKGeocodeSearch.h>

@interface SunTools () <BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>

@end

@implementation SunTools {
    BMKLocationService *_locationService;
    BMKGeoCodeSearch *_geoCodeSearch;
    BMKReverseGeoCodeOption *_geoCodeOption;
    
}

static SunTools *shareBMKTools = nil;

+ (instancetype)shareBMKTools {
    @synchronized(self) {
        if (!shareBMKTools) {
            shareBMKTools = [[self alloc] init];
            [shareBMKTools initService];
        }
    }
    //据说下面的方法更好
//    static dispatch_once_t once;
//    dispatch_once(&once, ^{
//        //
//        shareBMKTools = [[self alloc] init];
//    });
    return shareBMKTools;
}

- (void)initService {
    _locationService = [[BMKLocationService alloc] init];
    _locationService.delegate = self;
    _geoCodeSearch = [[BMKGeoCodeSearch alloc] init];
    _geoCodeSearch.delegate = self;
    _geoCodeOption = [[BMKReverseGeoCodeOption alloc] init];
    _locationService.distanceFilter = 100;
    _locationService.allowsBackgroundLocationUpdates = YES;
    [_locationService startUserLocationService];
}

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"定位成功"
                                                    message:nil
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil, nil];
    [alert show];
    if ([_delegate respondsToSelector:@selector(getUserLocation:)]) {
        [_delegate getUserLocation:userLocation.location.coordinate];
        
    }
    _geoCodeOption.reverseGeoPoint = userLocation.location.coordinate;
    [_geoCodeSearch reverseGeoCode:_geoCodeOption];
    
}

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    if (error == BMK_SEARCH_NO_ERROR) {
        if ([_delegate respondsToSelector:@selector(getUserAddress:detail:)]) {
            [_delegate getUserAddress:result.address detail:result.addressDetail];
        }
    }
}

+ (void)useBaiduTTSPlayString:(NSString *)string {
    NSInteger ret = [[BDTTSSynthesizer sharedInstance] speak:string];
}



@end
